class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    SetCriterion(item) {
        item['MaterialToleranceTypeName'] = formatNullInputValue($.trim($("#MaterialToleranceTypeName").val()));
        return item;
    }
    GetFilter(pageIndex) {
        let item = super.GetFilter(pageIndex);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    FetchAll() {
        $("#MaterialToleranceTypeName").val("");
        this.FetchForPage(1);
    }
    SetTableContent(result) {
        $("#tabData01 tbody").empty();
        let content_01 = '';
        this.rows = result.length;
        this.numCheckbox = result.length;  // 设置表格中checkbox的数量为表格的行数
        for (let i = 0; i < result.length; i++) {
            let id = result[i].typeId;
            content_01 += '<tr  _mash_id="' + id + '">';
            content_01 += '<td class="column__action"><span title="删除" class="trash" onclick="_Instance.DoDelete($(this));"><i class="fa-all fa-trash-o fa-1"></i></span></td>';
            content_01 += '<td class="edited-cell">' + result[i].typeNo + '</td>';
            content_01 += '<td class="edited-cell" style="border-right:none;">' + result[i].typeName + '</td>';
            content_01 += '</tr>';

            $("#tabData01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tabData01");

        let id = "cell__field";
        let name = "_cell__field";
        $('#tabData01 tbody tr').find("td.edited-cell").click(function () {
            OnCellClickEvent($(this), id, name, 0);
        });

        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    // 得到汇率明细
    GetLinesData() {
        let jsonInsert = [];
        let jsonUpdate = [];
        let itemResult = {};
        let jsonResult = [];
        // 得到修改过数据
        $('#tabData01 tbody').find("tr").each(function () {
            if ($(this).find("td.dirty").length) {
                let item = {};
                let $row = $(this);

                item['typeId'] = $row.attr('_mash_id');
                item['typeNo'] = $.trim($row.find('td').eq(1).text());
                item['typeName'] = $.trim($row.find('td').eq(2).text());
                item['createdBy'] = $("#UserId").val();
                item['updatedBy'] = $("#UserId").val();

                if (parseInt($row.attr("_mash_id")) === 0) {
                    jsonInsert.push(item);
                } else {
                    jsonUpdate.push(item);
                }
            }
        });
        itemResult['insertList'] = jsonInsert;
        itemResult['updateList'] = jsonUpdate;
        let _pageData = super.GetFilter(this.$pagination.attr("_msh_page"));
        _pageData = this.SetCriterion(_pageData);
        itemResult['pageData'] = _pageData;
        jsonResult.push(itemResult);

        return itemResult;
    }
    DoInsert() {
        this.InsertNewLines();
    }
    // 保存
    DoSave() {
        //step 1、进行页面验证，失败则返回
        let success_01 = CheckGridInput();
        if (!success_01)
            return;

        //step 2、执行处理
        super.DoAjax(this.GetLinesData(), "/master/material_tolerance_type/save.html", (data, $self) => {
            if (data.Success === true) {
                $self.BuildDataTable(data);
            } else {
                $self.EmptyTableContent();
            }
            ShowSuccessMessagePanel("保存成功。");
        });
    }
    DoDelete($self) {
        let _params = super.GetFilter(this.$pagination.attr("_msh_page"));
        // 设置条件
        _params = this.SetCriterion(_params);
        _params['TypeId'] = $self.parent().parent().attr("_mash_id");
        _params['TypeName'] = $self.parent().parent().find("td").eq(2).text();
        _params['DeletedBy'] = $("#UserId").val();

        super.DoDelete(_params, function (_this, data) {
            if (data.Success === true) {
                _this.BuildDataTable(data);
            } else {
                _this.EmptyTableContent();
            }
            ShowSuccessMessagePanel('删除成功。');
        });
    }
    // 添加新行以及新行的操作方式
    InsertNewLines() {
        let $newLine = $('<tr class="newLine" _mash_id="0" >' +
                '    <td style="text-align:center;"><span title="删除" class="trash" onclick="_Instance.RemoveNewLine($(this));"><i class="fa-all fa-trash-o fa-1"></i></span></td>' +
                '    <td class="edited-cell"></td>' +
                '    <td class="edited-cell" style="border-right:none;"></td>' +
                '</tr>');
        $('#tabData01 tbody').prepend($newLine);   // 加在前面
        this.rows = this.rows + 1;

        setOneTableRowColor("tabData01");

        let id = "cell__field";
        let name = "_cell__field";
        $newLine.find("td.edited-cell").click(function () {
            OnCellClickEvent($(this), id, name, 0);
        });
    }
    RemoveNewLine($self) {
        showModal(350, 140, '删除确认', '', null, null, 1, '请问，确定要删除当前行吗？', () => {
            $self.parent().parent().remove();
            this.rows = this.rows - 1;
        });
    }
    SortTable(orderBy) {
        this.orderby = orderBy;
        // 得到排序数据
        this.FetchForPage(1);
    }
}
let _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    _Instance = new MyPage(0, null, null, $("#page-size"), $("abbr.page_indicator_block,#wrapper__navbar"), null, null, null,
            "/master/material_tolerance_type/fetchForPage.html",
            "/master/material_tolerance_type/delete.html");

    $("#wrapper__navbar").swPagination({
        link_string: '/?page={page_number}',
        max_page: null,
        paged: function (page) {
            $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page", page);
            _Instance.FetchForPage(page);
        }
    });
    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}

//-----------------------------------------------------------------------------
// 验证函数
//-----------------------------------------------------------------------------
//验证输入
function CheckGridInput() {
    $('#tabData01 tbody').find("tr").each(function () {
        let $row = $(this);
        let _TypeNo = $.trim($(this).find('td').eq(1).text());     // 类型编号
        let _TypeName = $.trim($(this).find('td').eq(2).text());   // 类型名

        if (_TypeNo === '') {
            GridCheckErrHandle($row, 1, '请输编号。');
        } else {
            GridCheckSuccessHandle($row, 1);
        }

        if (_TypeName === '') {
            GridCheckErrHandle($row, 2, '请输入类型名。');
        } else {
            GridCheckSuccessHandle($row, 2);
        }
    });
    if ($('#tabData01 tbody').find('td.error').length > 0) {
        ShowErrorMessagePanel('对不起，填写的数据有错误。');
        return false;
    } else {
        return true;
    }
}
function GridCheckErrHandle($row, index, errMsg) {
    $row.find('td').eq(index).addClass("error");
    $row.find('td').eq(index).attr('title', errMsg);
}
function GridCheckSuccessHandle($row, index) {
    $row.find('td').eq(index).removeClass("error");
    $row.find('td').eq(index).attr('title', '');
}